import re
from collections import Counter
english_freq = {
    'a': 0.08167, 'b': 0.01492, 'c': 0.02782, 'd': 0.04253, 'e': 0.12702, 'f': 0.02228, 'g': 0.02015,
    'h': 0.06094, 'i': 0.06966, 'j': 0.00153, 'k': 0.00772, 'l': 0.04025, 'm': 0.02406, 'n': 0.06749,
    'o': 0.07507, 'p': 0.01929, 'q': 0.00095, 'r': 0.05987, 's': 0.06327, 't': 0.09056, 'u': 0.02758,
    'v': 0.00978, 'w': 0.02360, 'x': 0.00150, 'y': 0.01974, 'z': 0.00074
}
def shengcheng1():
 jiamibiao=str.maketrans(alphabet,str1)
 return jiamibiao

def shengcheng2():
 jiemibiao=str.maketrans(str1,alphabet)
 return jiemibiao

def encrypt2(text):
 cipher_table = shengcheng1()
 encrypted_text = text.translate(cipher_table)
 return encrypted_text

def decrypt2(text):
 cipher_table = shengcheng2()
 decrypted_text = text.translate(cipher_table)
 return decrypted_text

def generate_cipher_table(shift):        # 凯撒密码代换，偏移量k=shift
 shifted_alphabet = alphabet[shift:] + alphabet[:shift]
 cipher_table = str.maketrans(alphabet, shifted_alphabet)
 return cipher_table

def encrypt1(text, shift):
 cipher_table = generate_cipher_table(shift)
 encrypted_text = text.translate(cipher_table)
 return encrypted_text

def decrypt1(text, shift):
 cipher_table = generate_cipher_table(-shift)
 decrypted_text = text.translate(cipher_table)
 return decrypted_text

def analyze_frequency(ciphertext):
 cipher_freq = Counter(ciphertext.lower())
 total_chars = sum(cipher_freq.values())
 cipher_freq = {char: (count / total_chars) for char, count in cipher_freq.items() if char.isalpha()}
 return cipher_freq

def suggest_key(cipher_freq):
 # 根据频率匹配建议密钥
 suggested_key = {}
 for cipher_char, cipher_prob in cipher_freq.items():
  best_match = min(english_freq, key=lambda x: abs(english_freq[x] - cipher_prob))
  suggested_key[cipher_char] = best_match
 return suggested_key

print("欢迎使用单表代换密码辅助工具，请选择你需要的功能:")
print("1.凯撒密码加密解密器")
print("2.单表代换密码加密解密器")
print("3.单表代换密文辅助分析工具")
k=int(input())
alphabet = 'abcdefghijklmnopqrstuvwxyz'
if k==1:
 print("请输入凯撒密码偏移量：")
 shift=int(input())
 print("请选择功能：")
 print("1.加密")
 print("2.解密")
 t=int(input())
 if t==1:
  print("请输入明文（小写字母）：")
  original_text = input()
  encrypted_text = encrypt1(original_text, shift)
  print("加密得到的密文为:")
  print(encrypted_text)
 else:
  print("请输入密文(小写字母)：")
  encrypted_text=input()
  decrypted_text = decrypt1(encrypted_text, shift)
  print("解密得到的明文为：")
  print(decrypted_text)

if k==2:
 print("请输入单表代换密码表：(从小写a-小写z的顺序依次输入)")  # 单表代换密码表生成
 str1 = ''
 for i in range(0, 26):
  str1 = str1 + input()
 print("明文密文小写字母对应关系如下：")
 print(alphabet)
 print(str1)
 print("请选择功能：")
 print("1.加密")
 print("2.解密")
 x=int(input())
 if x==1:
  str2 = input("请输入明文（小写字母）：")
  miwen = encrypt2(str2)
  print("明文密文小写字母对应关系如下：")
  print(alphabet)
  print(str1)
  print("加密结果如下：")
  print(miwen)
 else:
  miwen = input("请输入密文（小写字母）:")
  mingwen=decrypt2(miwen)
  print("明文密文小写字母对应关系如下：")
  print(alphabet)
  print(str1)
  print("解密结果如下：")
  print(mingwen)
if k==3:
 print("请输入需要需要分析的密文：")
 str3=input()
 frequency = analyze_frequency(str3)
 fenxi1=suggest_key(frequency)
 print("根据频率第一次分析密文可能对应的明文如下")
 print(fenxi1)